package com.travel.group.controller;

import com.travel.group.common.PageResult;
import com.travel.group.entity.Comment;
import com.travel.group.entity.Group;
import com.travel.group.service.CommentService;
import com.travel.group.utils.WeChatResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @RequestMapping("/comment/toPage")
    public String toPage(){
        return "comment/list";
    }

    //添加评论
    @RequestMapping("wx/addComment")
    @ResponseBody
    public Object addComment(@RequestBody Comment comment){
        comment.setCreateDate(new Date());
        if (commentService.save(comment))
            return WeChatResponseUtil.ok();
        return WeChatResponseUtil.fail();
    }

    //获取所有评价
    @RequestMapping("wx/getAllCommentsList")
    @ResponseBody
    public PageResult<Comment> getAllCommentsList(Integer page, Integer limit, @RequestParam("number") String number){
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 10;
        }
        List<Comment> allArticle = commentService.getAllCommentInfoPlus(page, limit, number);
        Integer count = allArticle.size();
        PageResult<Comment> res = new PageResult<>(allArticle, count);
        return res;
    }

    //删除评论
    @RequestMapping("wx/deleteComment")
    @ResponseBody
    public Object deleteComment(@RequestBody Comment comment){
        if (commentService.removeById(comment.getId()))
            return WeChatResponseUtil.ok();
        return WeChatResponseUtil.fail();
    }

    //获取所有评价
    @RequestMapping("wx/getAllComments")
    @ResponseBody
    public PageResult<Comment> getCommodityComments(Integer page, Integer limit){
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 10;
        }
        List<Comment> allArticle = commentService.getAllCommentInfo(page, limit);
        Integer count = commentService.countData();
        PageResult<Comment> res = new PageResult<>(allArticle, count);
        return res;
    }

    //根据 id 进行删除操作
    @RequestMapping(value = "/comment/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public PageResult delete(@PathVariable("id") Integer id){
        if(commentService.removeById(id)){
            return new PageResult("删除成功",200);
        }
        return new PageResult("删除失败",400);
    }

    //审核信息
    @RequestMapping("/comment/check/{id}")
    @ResponseBody
    public PageResult check(@PathVariable("id") Integer id){
        Comment comment = commentService.getById(id);
        comment.setStatus(1);
        if(commentService.updateById(comment)){
            return new PageResult("审核成功",200);
        }
        return new PageResult("审核失败",400);
    }
}
